A virtual
function has an implementation that may be replaced in a child class. A pure virtual
has no
implementation, and must be implemented in child classes.
Hiding a base class implementation with a "pure implementation" (=0
) is sure to confuse extenders, who may not be aware of the base
class' implementation. Instead, they’ll see there’s no implementation in the class they’re extending and assume that none exists. When that base class
implementation contains crucial functionality such as freeing resources, this could cause future users of the class to introduce bugs.
This rule raises an issue if a pure virtual function overrides a virtual function that is not pure.
Noncompliant code example
struct A {
virtual void func1();
virtual void func2() = 0;
};
struct B : A {
virtual void func1() = 0; // Noncompliant; override non-pure virtual
virtual void func2() = 0; // Compliant; but useless
};
Compliant solution
struct A {
virtual void func1();
virtual void func2() = 0;
};
struct B : A {
virtual void func1(); // Compliant; non-pure virtual
};